package com.lqian.tcp;

import java.io.*;
import java.net.Socket;

public class ServerHandleThread implements Runnable {
    Socket socket = null;

    //构造器初始化
    public ServerHandleThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        OutputStream os = null;
        PrintWriter pw = null;

        try {
            //获取socket输入流
            InputStream is = socket.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            //readObject()方法必须保证服务端和客户端的User包名一致，要不然会出现找不到类的错误
            System.out.println("客户端发送的对象：" + (User) ois.readObject());
            socket.shutdownInput();// 禁用套接字的输入流
            os=socket.getOutputStream();
            //输出流
            pw=new PrintWriter(os);
            pw.println("欢迎登录！");
            pw.flush();
            socket.shutdownOutput();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (pw != null) {
                    pw.close();
                }
                if (os != null) {
                    os.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
